package o;

import android.util.SparseArray;
import android.util.SparseBooleanArray;
import com.google.android.exoplayer2.upstream.cache.Cache.CacheException;
import java.io.File;
import java.security.NoSuchAlgorithmException;
import java.util.Collection;
import java.util.HashMap;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

class amk {
    private ani ʻ;
    private final SecretKeySpec ʼ;
    private boolean ʽ;
    private final HashMap<String, amc> ˊ;
    private final Cipher ˋ;
    private final SparseArray<String> ˎ;
    private final amo ˏ;
    private final SparseBooleanArray ॱ;
    private final boolean ᐝ;

    public amk(File file, byte[] bArr, boolean z) {
        Throwable e;
        boolean z2 = true;
        this.ᐝ = z;
        if (bArr != null) {
            if (bArr.length != 16) {
                z2 = false;
            }
            amm.ˊ(z2);
            try {
                this.ˋ = ᐝ();
                this.ʼ = new SecretKeySpec(bArr, "AES");
            } catch (NoSuchAlgorithmException e2) {
                e = e2;
                throw new IllegalStateException(e);
            } catch (NoSuchPaddingException e3) {
                e = e3;
                throw new IllegalStateException(e);
            }
        }
        if (z) {
            z2 = false;
        }
        amm.ˏ(z2);
        this.ˋ = null;
        this.ʼ = null;
        this.ˊ = new HashMap();
        this.ˎ = new SparseArray();
        this.ॱ = new SparseBooleanArray();
        this.ˏ = new amo(new File(file, "cached_content_index.exi"));
    }

    public void ॱ() {
        amm.ˏ(!this.ʽ);
        if (!ˎ()) {
            this.ˏ.ॱ();
            this.ˊ.clear();
            this.ˎ.clear();
        }
    }

    public void ˊ() throws CacheException {
        int i = 0;
        if (this.ʽ) {
            ॱॱ();
            this.ʽ = false;
            int size = this.ॱ.size();
            while (i < size) {
                this.ˎ.remove(this.ॱ.keyAt(i));
                i++;
            }
            this.ॱ.clear();
        }
    }

    public amc ˊ(String str) {
        amc o_amc = (amc) this.ˊ.get(str);
        return o_amc == null ? ᐝ(str) : o_amc;
    }

    public amc ˏ(String str) {
        return (amc) this.ˊ.get(str);
    }

    public Collection<amc> ˏ() {
        return this.ˊ.values();
    }

    public int ˋ(String str) {
        return ˊ(str).ॱ;
    }

    public String ˏ(int i) {
        return (String) this.ˎ.get(i);
    }

    public void ˎ(String str) {
        amc o_amc = (amc) this.ˊ.get(str);
        if (o_amc != null && o_amc.ॱ() && !o_amc.ˎ()) {
            this.ˊ.remove(str);
            this.ʽ = true;
            this.ˎ.put(o_amc.ॱ, null);
            this.ॱ.put(o_amc.ॱ, true);
        }
    }

    public void ˋ() {
        String[] strArr = new String[this.ˊ.size()];
        this.ˊ.keySet().toArray(strArr);
        for (String ˎ : strArr) {
            ˎ(ˎ);
        }
    }

    public void ॱ(String str, ami o_ami) {
        if (ˊ(str).ˎ(o_ami)) {
            this.ʽ = true;
        }
    }

    public amh ॱ(String str) {
        amc ˏ = ˏ(str);
        return ˏ != null ? ˏ.ˏ() : amj.ˎ;
    }

    /* JADX WARNING: inconsistent code. */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    private boolean ˎ() {
        /*
        r9 = this;
        r6 = 2;
        r1 = 1;
        r0 = 0;
        r3 = 0;
        r4 = new java.io.BufferedInputStream;	 Catch:{ IOException -> 0x00ad, all -> 0x00aa }
        r2 = r9.ˏ;	 Catch:{ IOException -> 0x00ad, all -> 0x00aa }
        r2 = r2.ˋ();	 Catch:{ IOException -> 0x00ad, all -> 0x00aa }
        r4.<init>(r2);	 Catch:{ IOException -> 0x00ad, all -> 0x00aa }
        r2 = new java.io.DataInputStream;	 Catch:{ IOException -> 0x00ad, all -> 0x00aa }
        r2.<init>(r4);	 Catch:{ IOException -> 0x00ad, all -> 0x00aa }
        r5 = r2.readInt();	 Catch:{ IOException -> 0x0073, all -> 0x0083 }
        if (r5 < 0) goto L_0x001c;
    L_0x001a:
        if (r5 <= r6) goto L_0x0022;
    L_0x001c:
        if (r2 == 0) goto L_0x0021;
    L_0x001e:
        o.anr.ॱ(r2);
    L_0x0021:
        return r0;
    L_0x0022:
        r3 = r2.readInt();	 Catch:{ IOException -> 0x0073, all -> 0x0083 }
        r3 = r3 & 1;
        if (r3 == 0) goto L_0x007b;
    L_0x002a:
        r3 = r9.ˋ;	 Catch:{ IOException -> 0x0073, all -> 0x0083 }
        if (r3 != 0) goto L_0x0034;
    L_0x002e:
        if (r2 == 0) goto L_0x0021;
    L_0x0030:
        o.anr.ॱ(r2);
        goto L_0x0021;
    L_0x0034:
        r3 = 16;
        r3 = new byte[r3];	 Catch:{ IOException -> 0x0073, all -> 0x0083 }
        r2.readFully(r3);	 Catch:{ IOException -> 0x0073, all -> 0x0083 }
        r6 = new javax.crypto.spec.IvParameterSpec;	 Catch:{ IOException -> 0x0073, all -> 0x0083 }
        r6.<init>(r3);	 Catch:{ IOException -> 0x0073, all -> 0x0083 }
        r3 = r9.ˋ;	 Catch:{ InvalidKeyException -> 0x00b3, InvalidAlgorithmParameterException -> 0x006c }
        r7 = 2;
        r8 = r9.ʼ;	 Catch:{ InvalidKeyException -> 0x00b3, InvalidAlgorithmParameterException -> 0x006c }
        r3.init(r7, r8, r6);	 Catch:{ InvalidKeyException -> 0x00b3, InvalidAlgorithmParameterException -> 0x006c }
        r3 = new java.io.DataInputStream;	 Catch:{ IOException -> 0x0073, all -> 0x0083 }
        r6 = new javax.crypto.CipherInputStream;	 Catch:{ IOException -> 0x0073, all -> 0x0083 }
        r7 = r9.ˋ;	 Catch:{ IOException -> 0x0073, all -> 0x0083 }
        r6.<init>(r4, r7);	 Catch:{ IOException -> 0x0073, all -> 0x0083 }
        r3.<init>(r6);	 Catch:{ IOException -> 0x0073, all -> 0x0083 }
        r2 = r3;
    L_0x0055:
        r6 = r2.readInt();	 Catch:{ IOException -> 0x00b0, all -> 0x0083 }
        r3 = r0;
        r4 = r0;
    L_0x005b:
        if (r3 >= r6) goto L_0x008a;
    L_0x005d:
        r7 = o.amc.ˎ(r5, r2);	 Catch:{ IOException -> 0x00b0, all -> 0x0083 }
        r9.ˊ(r7);	 Catch:{ IOException -> 0x00b0, all -> 0x0083 }
        r7 = r7.ˏ(r5);	 Catch:{ IOException -> 0x00b0, all -> 0x0083 }
        r4 = r4 + r7;
        r3 = r3 + 1;
        goto L_0x005b;
    L_0x006c:
        r1 = move-exception;
    L_0x006d:
        r3 = new java.lang.IllegalStateException;	 Catch:{ IOException -> 0x0073, all -> 0x0083 }
        r3.<init>(r1);	 Catch:{ IOException -> 0x0073, all -> 0x0083 }
        throw r3;	 Catch:{ IOException -> 0x0073, all -> 0x0083 }
    L_0x0073:
        r1 = move-exception;
        r1 = r2;
    L_0x0075:
        if (r1 == 0) goto L_0x0021;
    L_0x0077:
        o.anr.ॱ(r1);
        goto L_0x0021;
    L_0x007b:
        r3 = r9.ᐝ;	 Catch:{ IOException -> 0x0073, all -> 0x0083 }
        if (r3 == 0) goto L_0x0055;
    L_0x007f:
        r3 = 1;
        r9.ʽ = r3;	 Catch:{ IOException -> 0x0073, all -> 0x0083 }
        goto L_0x0055;
    L_0x0083:
        r0 = move-exception;
    L_0x0084:
        if (r2 == 0) goto L_0x0089;
    L_0x0086:
        o.anr.ॱ(r2);
    L_0x0089:
        throw r0;
    L_0x008a:
        r5 = r2.readInt();	 Catch:{ IOException -> 0x00b0, all -> 0x0083 }
        r3 = r2.read();	 Catch:{ IOException -> 0x00b0, all -> 0x0083 }
        r6 = -1;
        if (r3 != r6) goto L_0x00a0;
    L_0x0095:
        r3 = r1;
    L_0x0096:
        if (r5 != r4) goto L_0x009a;
    L_0x0098:
        if (r3 != 0) goto L_0x00a2;
    L_0x009a:
        if (r2 == 0) goto L_0x0021;
    L_0x009c:
        o.anr.ॱ(r2);
        goto L_0x0021;
    L_0x00a0:
        r3 = r0;
        goto L_0x0096;
    L_0x00a2:
        if (r2 == 0) goto L_0x00a7;
    L_0x00a4:
        o.anr.ॱ(r2);
    L_0x00a7:
        r0 = r1;
        goto L_0x0021;
    L_0x00aa:
        r0 = move-exception;
        r2 = r3;
        goto L_0x0084;
    L_0x00ad:
        r1 = move-exception;
        r1 = r3;
        goto L_0x0075;
    L_0x00b0:
        r1 = move-exception;
        r1 = r2;
        goto L_0x0075;
    L_0x00b3:
        r1 = move-exception;
        goto L_0x006d;
        */
        throw new UnsupportedOperationException("Method not decompiled: o.amk.ˎ():boolean");
    }

    /* JADX WARNING: inconsistent code. */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    private void ॱॱ() throws com.google.android.exoplayer2.upstream.cache.Cache.CacheException {
        /*
        r7 = this;
        r1 = 0;
        r3 = 1;
        r0 = 0;
        r2 = r7.ˏ;	 Catch:{ IOException -> 0x0089, all -> 0x00ae }
        r2 = r2.ˊ();	 Catch:{ IOException -> 0x0089, all -> 0x00ae }
        r4 = r7.ʻ;	 Catch:{ IOException -> 0x0089, all -> 0x00ae }
        if (r4 != 0) goto L_0x0083;
    L_0x000d:
        r4 = new o.ani;	 Catch:{ IOException -> 0x0089, all -> 0x00ae }
        r4.<init>(r2);	 Catch:{ IOException -> 0x0089, all -> 0x00ae }
        r7.ʻ = r4;	 Catch:{ IOException -> 0x0089, all -> 0x00ae }
    L_0x0014:
        r2 = new java.io.DataOutputStream;	 Catch:{ IOException -> 0x0089, all -> 0x00ae }
        r4 = r7.ʻ;	 Catch:{ IOException -> 0x0089, all -> 0x00ae }
        r2.<init>(r4);	 Catch:{ IOException -> 0x0089, all -> 0x00ae }
        r4 = 2;
        r2.writeInt(r4);	 Catch:{ IOException -> 0x009f, all -> 0x00b1 }
        r4 = r7.ᐝ;	 Catch:{ IOException -> 0x009f, all -> 0x00b1 }
        if (r4 == 0) goto L_0x0096;
    L_0x0023:
        r2.writeInt(r3);	 Catch:{ IOException -> 0x009f, all -> 0x00b1 }
        r3 = r7.ᐝ;	 Catch:{ IOException -> 0x009f, all -> 0x00b1 }
        if (r3 == 0) goto L_0x0058;
    L_0x002a:
        r3 = 16;
        r3 = new byte[r3];	 Catch:{ IOException -> 0x009f, all -> 0x00b1 }
        r4 = new java.util.Random;	 Catch:{ IOException -> 0x009f, all -> 0x00b1 }
        r4.<init>();	 Catch:{ IOException -> 0x009f, all -> 0x00b1 }
        r4.nextBytes(r3);	 Catch:{ IOException -> 0x009f, all -> 0x00b1 }
        r2.write(r3);	 Catch:{ IOException -> 0x009f, all -> 0x00b1 }
        r4 = new javax.crypto.spec.IvParameterSpec;	 Catch:{ IOException -> 0x009f, all -> 0x00b1 }
        r4.<init>(r3);	 Catch:{ IOException -> 0x009f, all -> 0x00b1 }
        r3 = r7.ˋ;	 Catch:{ InvalidKeyException -> 0x00b6, InvalidAlgorithmParameterException -> 0x0098 }
        r5 = 1;
        r6 = r7.ʼ;	 Catch:{ InvalidKeyException -> 0x00b6, InvalidAlgorithmParameterException -> 0x0098 }
        r3.init(r5, r6, r4);	 Catch:{ InvalidKeyException -> 0x00b6, InvalidAlgorithmParameterException -> 0x0098 }
        r2.flush();	 Catch:{ IOException -> 0x009f, all -> 0x00b1 }
        r3 = new java.io.DataOutputStream;	 Catch:{ IOException -> 0x009f, all -> 0x00b1 }
        r4 = new javax.crypto.CipherOutputStream;	 Catch:{ IOException -> 0x009f, all -> 0x00b1 }
        r5 = r7.ʻ;	 Catch:{ IOException -> 0x009f, all -> 0x00b1 }
        r6 = r7.ˋ;	 Catch:{ IOException -> 0x009f, all -> 0x00b1 }
        r4.<init>(r5, r6);	 Catch:{ IOException -> 0x009f, all -> 0x00b1 }
        r3.<init>(r4);	 Catch:{ IOException -> 0x009f, all -> 0x00b1 }
        r2 = r3;
    L_0x0058:
        r3 = r7.ˊ;	 Catch:{ IOException -> 0x00b3, all -> 0x00b1 }
        r3 = r3.size();	 Catch:{ IOException -> 0x00b3, all -> 0x00b1 }
        r2.writeInt(r3);	 Catch:{ IOException -> 0x00b3, all -> 0x00b1 }
        r3 = r7.ˊ;	 Catch:{ IOException -> 0x00b3, all -> 0x00b1 }
        r3 = r3.values();	 Catch:{ IOException -> 0x00b3, all -> 0x00b1 }
        r4 = r3.iterator();	 Catch:{ IOException -> 0x00b3, all -> 0x00b1 }
        r3 = r0;
    L_0x006c:
        r0 = r4.hasNext();	 Catch:{ IOException -> 0x00b3, all -> 0x00b1 }
        if (r0 == 0) goto L_0x00a2;
    L_0x0072:
        r0 = r4.next();	 Catch:{ IOException -> 0x00b3, all -> 0x00b1 }
        r0 = (o.amc) r0;	 Catch:{ IOException -> 0x00b3, all -> 0x00b1 }
        r0.ˊ(r2);	 Catch:{ IOException -> 0x00b3, all -> 0x00b1 }
        r5 = 2;
        r0 = r0.ˏ(r5);	 Catch:{ IOException -> 0x00b3, all -> 0x00b1 }
        r0 = r0 + r3;
        r3 = r0;
        goto L_0x006c;
    L_0x0083:
        r4 = r7.ʻ;	 Catch:{ IOException -> 0x0089, all -> 0x00ae }
        r4.ˏ(r2);	 Catch:{ IOException -> 0x0089, all -> 0x00ae }
        goto L_0x0014;
    L_0x0089:
        r0 = move-exception;
    L_0x008a:
        r2 = new com.google.android.exoplayer2.upstream.cache.Cache$CacheException;	 Catch:{ all -> 0x0090 }
        r2.<init>(r0);	 Catch:{ all -> 0x0090 }
        throw r2;	 Catch:{ all -> 0x0090 }
    L_0x0090:
        r0 = move-exception;
        r2 = r1;
    L_0x0092:
        o.anr.ॱ(r2);
        throw r0;
    L_0x0096:
        r3 = r0;
        goto L_0x0023;
    L_0x0098:
        r0 = move-exception;
    L_0x0099:
        r1 = new java.lang.IllegalStateException;	 Catch:{ IOException -> 0x009f, all -> 0x00b1 }
        r1.<init>(r0);	 Catch:{ IOException -> 0x009f, all -> 0x00b1 }
        throw r1;	 Catch:{ IOException -> 0x009f, all -> 0x00b1 }
    L_0x009f:
        r0 = move-exception;
        r1 = r2;
        goto L_0x008a;
    L_0x00a2:
        r2.writeInt(r3);	 Catch:{ IOException -> 0x00b3, all -> 0x00b1 }
        r0 = r7.ˏ;	 Catch:{ IOException -> 0x00b3, all -> 0x00b1 }
        r0.ˊ(r2);	 Catch:{ IOException -> 0x00b3, all -> 0x00b1 }
        o.anr.ॱ(r1);
        return;
    L_0x00ae:
        r0 = move-exception;
        r2 = r1;
        goto L_0x0092;
    L_0x00b1:
        r0 = move-exception;
        goto L_0x0092;
    L_0x00b3:
        r0 = move-exception;
        r1 = r2;
        goto L_0x008a;
    L_0x00b6:
        r0 = move-exception;
        goto L_0x0099;
        */
        throw new UnsupportedOperationException("Method not decompiled: o.amk.ॱॱ():void");
    }

    private amc ᐝ(String str) {
        amc o_amc = new amc(ˋ(this.ˎ), str);
        ˊ(o_amc);
        this.ʽ = true;
        return o_amc;
    }

    private void ˊ(amc o_amc) {
        this.ˊ.put(o_amc.ˋ, o_amc);
        this.ˎ.put(o_amc.ॱ, o_amc.ˋ);
    }

    private static Cipher ᐝ() throws NoSuchPaddingException, NoSuchAlgorithmException {
        if (anr.ˋ == 18) {
            try {
                return Cipher.getInstance("AES/CBC/PKCS5PADDING", "BC");
            } catch (Throwable th) {
            }
        }
        return Cipher.getInstance("AES/CBC/PKCS5PADDING");
    }

    public static int ˋ(SparseArray<String> sparseArray) {
        int size = sparseArray.size();
        int keyAt = size == 0 ? 0 : sparseArray.keyAt(size - 1) + 1;
        if (keyAt < 0) {
            keyAt = 0;
            while (keyAt < size && keyAt == sparseArray.keyAt(keyAt)) {
                keyAt++;
            }
        }
        return keyAt;
    }
}
